//编辑承运商

/**
 * 编辑承运商视图
 */
interface ICarrierManagentEditViewModel extends ng.IScope {
    /**
    * 承运商类型下拉
    */
    carrierTypeDropDown: ValueListItemResponse[];
    /**
     * 承运商等级下拉
     */
    carrierLevelDropDown: ValueListItemResponse[];
    /**
     * 承运商类型
     */
    carrierType: string;
    /**
     * 承运商名称
     */
    carrierName: string;
    /**
     * 承运商号码
     */
    carrierPhone: string;
    /**
     * 承运商地址
     */
    carrierAddress?: string;
    /**
     * 承运商等级
     */
    carrierLevel: string;
    /**
     * 备注
     */
    carrierRemarks?: string;
    /**
     * 保存
     */
    carrierSave: () => void;

    /**
     * 返回事件
     */
    backUrl:()=>void;
}

class CarrierManagentEditController {
    carrierId: string;

    constructor(private $scope: ICarrierManagentEditViewModel, private carrierService: ICarrierService,
        private valueService: IValueService, private $state: angular.ui.IStateService, private $ngBootbox: BootboxService, private $location: ng.ILocationService) {
        this.init();
    }

    init(): void {
        this.clearInput();
        this.carrierId = this.$location.search().id;
        /**
         * 判断carrierId是否存在,不存在则从本地存储中获取
         */
        if(!this.carrierId){
            this.carrierId = JSON.parse(window.localStorage.getItem("carrier").toString()).carrierId;
        }
        else{
            this.carrierId = this.carrierId;
        }

        /**
         * 本地存储承运商id
         */
        let carrier = {
            carrierId:this.carrierId,
        }
        window.localStorage.setItem("carrier",JSON.stringify(carrier));

        this.loadCarrierTypeDropDown();
        this.loadCarrierLevelDropDown();
        this.$scope.carrierSave = this.editCarrier;
        this.loadData();

        this.$scope.backUrl = this.backUrl;
    }

    /**
     * 下拉承运商类别事件
     */
    loadCarrierTypeDropDown = ()=> {
        this.$scope.carrierTypeDropDown = this.valueService.getCarrierTypeList().data
    }

    /**
     * 下拉承运商级别事件
     */
    loadCarrierLevelDropDown = ()=> {
        this.$scope.carrierLevelDropDown = this.valueService.getCarrierLevelList().data
    }

    /**
     *初始化表单
     */
    loadData = ()=> {
        this.carrierService.getDetail(this.carrierId).then(result => {
            this.$scope.carrierName = result.data.name;
            this.$scope.carrierType = result.data.type;
            this.$scope.carrierPhone = result.data.phone;
            this.$scope.carrierLevel = result.data.level;
            this.$scope.carrierAddress = result.data.address;
            this.$scope.carrierRemarks = result.data.remarks
        })
    }

    /**
     *编辑保存
     */
    editCarrier = () => {
        if (!$("#form").valid()) { return; }
        this.$ngBootbox.confirm("确定保存数据吗？").then(result => {
            this.carrierService.edit(
                this.carrierId,
                this.$scope.carrierName,
                this.$scope.carrierType,
                this.$scope.carrierPhone,
                this.$scope.carrierLevel,
                this.$scope.carrierAddress,
                this.$scope.carrierRemarks
            ).then(result => {
                this.$state.go("app.carrier.carrierManagent", { carrierId: result.data.extData });
            })
        })
    }

    clearInput = ()=> {
        this.$scope.carrierName = "";
        this.$scope.carrierType = "";
        this.$scope.carrierPhone = "";
        this.$scope.carrierLevel = "";
        this.$scope.carrierAddress = "";
        this.$scope.carrierRemarks = "";
        this.$scope.carrierTypeDropDown = [];
        this.$scope.carrierLevelDropDown = [];
    }

    backUrl=()=>{
        this.$state.go('app.carrier.carrierManagent',{name:'carrierManagent'})
    }
}

angular.module("tms").controller("carrierManagentEditController", ["$scope", "carrierService", "valueService", "$state", "$ngBootbox", "$location", CarrierManagentEditController]);